package com.ysg.widget.dialog;

import android.content.Context;
import android.graphics.Color;

import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;
import com.ysg.R;
import com.ysg.base.BaseDialog;

import org.jetbrains.annotations.NotNull;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * SVGA
 */
public class SVGADialog extends BaseDialog {
    private SVGAImageView svgaImageView;
    private String url;
    public SVGADialog(Context context, String url) {
        super(context);
        this.url = url;
    }


    @Override
    public int getLayoutId() {
        return R.layout.dialog_svga;
    }

    public void initView() {
        svgaImageView = findViewById(R.id.svgaImageView);
        setWidthScale(1f);
        setAlpha(1f);
    }


    @Override
    public void initData() {
        try {
            SVGAParser svgaParser = SVGAParser.Companion.shareParser();
//            svgaParser.setFrameSize(300,300);
            svgaParser.decodeFromURL(new URL(url), new SVGAParser.ParseCompletion() {
                @Override
                public void onComplete(@NotNull SVGAVideoEntity videoItem) {
                    svgaImageView.setVideoItem(videoItem);
                    svgaImageView.setBackgroundColor(Color.TRANSPARENT);
                    svgaImageView.startAnimation();
                    svgaImageView.setCallback(new SVGACallback() {
                        @Override
                        public void onPause() {
                        }

                        @Override
                        public void onFinished() {
                            svgaImageView.clear();
                            dismiss();
                        }

                        @Override
                        public void onRepeat() {
                        }

                        @Override
                        public void onStep(int i, double v) {
                        }
                    });
                }

                @Override
                public void onError() {
                    svgaImageView.clear();
                    dismiss();
                }
            }, null);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            svgaImageView.clear();
            dismiss();
        }
    }

    @Override
    public void initListener() {
    }


}
